# 平均值，中位数和模式
# Mean平均值
# # 创建向量
# x <- c(12, 7, 3, 4.2, 18, 2, 54, -21, 8, -5)
# # 执行
# result.mean <- mean(x)
# print(result.mean)

# # 创建向量
# x <- c(12, 7, 3, 4.2, 18, 2, 54, -21, 8, -5)
# # 执行
# result.mean <- mean(x)
# print(result.mean)
# # 修剪选项
# result.mean <- mean(x, trim = 0.3)
# print(result.mean)

# # NA选项--如果有缺失值，则平均函数返回NA
# x <- c(12, 7, 3, 4.2, 18, 2, 54, -21, 8, -5, NA)
# result.mean <- mean(x)
# print(result.mean)

# result.mean <- mean(x, na.rm = TRUE)
# print(result.mean)

# Median中位数
# # 创建向量
# x <- c(12, 7, 3, 4.2, 18, 2, 54, -21, 8, -5)
# # 查找中位数
# median.result <- median(x)
# print(median.result)

# 模式
# # 创建函数
# getmode <- function(v) {
# 	uniqv <- unique(v)
# 	uniqv[which.max(tabulate(match(v, uniqv)))]
# }
# # 创建向量
# v <- c(2, 1, 2, 3, 1, 2, 3, 4, 1, 5, 5, 3, 2, 3)
# # 计算模式
# result <- getmode(v)
# print(result)

# # 创建字符向量
# charv <- c("o", "it", "the", "it", "it")
# # 获取模式
# result <- getmode(charv)
# print(result)


# 线性回归
# # 创建关系模型并获取系数
# x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
# y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# # 应用lm()函数
# relation <- lm(y~x)
# print(relation)

# # 获取相关摘要
# x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
# y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# # 应用lm()函数
# relation <- lm(y~x)
# print(summary(relation))

# # 预测新人体重
# x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
# y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# # 应用lm()函数
# relation <- lm(y~x)
# # 身高170，预测体重
# a <- data.frame(x = 170)
# result <- predict(relation, a)
# print(result)

# # 图形方式可视化回归
# x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
# y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
# # 应用lm()函数
# relation <- lm(y~x)
# # 设置文件名
# png(file = "linearregression.png")
# # 绘制图表
# # 绘制点
# plot(y, x, col = "blue", main = "Height & Weight Regression")
# # 绘制线
# abline(lm(x~y), cex = 1.3, pch = 16, xlab = "Weight in Kg", ylab = "Height in cm")

# # 保存文件
# dev.off()

# 多重回归
# # 创建模型并获取系数
# input <- mtcars[,c("mpg", "disp", "hp", "wt")]
# # 创建关系模型
# model <- lm(mpg~disp+hp+wt, data = input)
# # 显示模型
# print(model)
# # 打印系数
# cat("# # # # The Coefficient Values # # # ","
# ")
# # 打印系数
# a <- coef(model)[1]
# print(a)
# Xdisp <- coef(model)[2]
# Xhp <- coef(model)[3]
# Xwt <- coef(model)[4]
# print(Xdisp)
# print(Xhp)
# print(Xwt)

# 逻辑回归
# # 创建回归模型
# input <- mtcars[,c("am", "cyl", "hp", "wt")]
# am.data = glm(formula = am ~ cyl + hp + wt, data = input, family = binomial)
# print(summary(am.data))

# 标准分布
# # dnorm() - 该函数给出给定平均值和标准偏差在每个点的概率分布的高度。
# x <- seq(-10, 10, .1)
# y <- dnorm(x, mean = 2.5, sd = 0.5)
# # 设置文件名
# png(file = "dnorm.png")
# # 绘制点
# plot(x,y)
# dev.off()

# # pnorm() - 该函数给出正态分布随机数的概率小于给定数的值。 它也被称为“累积分布函数”。
# x <- seq(-10, 10, .2)
# y <- pnorm(x, mean = 2.5, sd = 2)
# # 设置文件名
# png(file = "pnorm.png")
# # 绘制点
# plot(x, y)
# dev.off()

# # qnorm() - 该函数采用概率值，并给出累积值与概率值匹配的数字。
# x <- seq(0, 1, by = 0.02)
# y <- qnorm(x, mean = 2, sd = 1)
# # 设置文件名
# png(file = "qnorm.png")
# plot(x, y)
# dev.off()

# # rnorm() - 此函数用于生成分布正常的随机数。 它将样本大小作为输入，并生成许多随机数。 我们绘制一个直方图来显示生成的数字的分布。
# y = rnorm(50)
# png(file = "rnorm.png")
# hist(y, main = "Normal Distribution")
# dev.off()

# 二项分布
# # dbinom() - 该函数给出每个点的概率密度分布。
# # 创建数据
# x = seq(0, 50, 1)
# y = dbinom(x, 50, 0.5)
# png(file = "dbinom.png")
# plot(x, y)
# dev.off()

# # pbinom() - 此函数给出事件的累积概率。 它是表示概率的单个值。
# x <- pbinom(26, 51, 0.5)
# print(x)

# # qbinom() - 该函数采用概率值，并给出累积值与概率值匹配的数字。
# x <- qbinom(0.25, 51, 1/2)
# print(x)

# # rbinom() - 该函数从给定样本产生给定概率的所需数量的随机值。
# x <- rbinom(8, 150, .4)
# print(x)

# # 泊松分布
# # 创建回归模型
# # 内置的数据集“warpbreaks”
# output <- glm(formula = breaks ~ wool + tension,
# 		data = warpbreaks,
# 		family = poisson
# 	)
# print(summary(output))

# 协方差分析
# # 模型与分类变量和预测变量之间的相互作用
# result <- aov(mpg~hp*am, data = mtcars)
# print(summary(result))

# # 没有分类变量和预测变量之间相互作用的模型
# result <- aov(mpg~hp+am, data = mtcars)
# print(summary(result))

# # 比较两个模型
# input <- mtcars
# result1 <- aov(mpg~hp*am, data = input)
# result2 <- aov(mpg~hp+am, data = input)
# # 比较两个模型
# print(anova(result1, result2))

# 时间序列
# # 单时间序列
# rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
# # 转换为时间序列对象
# rainfall.timeseries <- ts(rainfall, start = c(2012, 1), frequency = 12)
# print(rainfall.timeseries)
# # 设置图片
# png(file = "rainfall.png")
# # 绘图
# plot(rainfall.timeseries)
# # 保存图片
# dev.off()

# # 多时间序列
# rainfall1 <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
# rainfall2 <- c(655,1306.9,1323.4,1172.2,562.2,824,822.4,1265.5,799.6,1105.6,1106.7,1337.8)
# # 转换为矩阵
# combined.rainfall <- matrix(c(rainfall1, rainfall2), nrow = 12)
# # 转换为时间序列
# rainfall.timeseries <- ts(combined.rainfall, start = c(2012, 1), frequency = 12)
# print(rainfall.timeseries)
# # 设置文件名
# png(file = "rainfall_combined.png")
# # 绘制点
# plot(rainfall.timeseries, main = "Multiple Time Series")
# # 保存文件
# dev.off()

# # 非线性最小二乘法
# xvalues <- c(1.6,2.1,2,2.23,3.71,3.25,3.4,3.86,1.19,2.21)
# yvalues <- c(5.19,7.43,6.94,8.11,18.75,14.88,16.06,19.12,3.21,7.58)
# # 设置图片
# png(file = "nls.png")
# # 绘图
# plot(xvalues, yvalues)
# # 取假设值并适合模型。
# model <- nls(yvalues ~ b1 * xvalues^2 + b2, start = list(b1 = 1, b2 = 3))
# # 绘制图表
# new.data <- data.frame(xvalues = seq(min(xvalues), max(xvalues), len = 100))
# lines(new.data$xvalues, predict(model, newdata = new.data))
# # 保存文件
# dev.off()
# # 获取model平法和
# print(sum(resid(model)^2))
# # 获得所选系数值的置信区间。
# print(confint(model))

# 决策树
# # 安装包 -- party包中用于创建和分析决策树的函数ctree()
# install.packages("party", repos="https://cran.cnr.berkeley.edu/")

# # 创建决策树
# library(party)
# # 创建输入数据帧
# input.data <- readingSkills[c(1:105),]
# # 设置文件名
# png(file = "decision_tree.png")
# # 创建树
# output.tree <- ctree(
# 		nativeSpeaker ~ age + shoeSize + score,
# 		data = input.data
# 	)
# # 绘图
# plot(output.tree)
# # 保存文件
# dev.off()

# 随机森林算法
# # 安装包 -- randomForest具有函数randomForest()，用于创建和分析随机森林。
# install.packages("randomForest", repos="https://cran.cnr.berkeley.edu/")

# library(party)
# library(randomForest)

# # 创建森林
# output.forest <- randomForest(nativeSpeaker ~ age + shoeSize + score, data = readingSkills)
# print(output.forest)
# print(importance(output.forest, type = 2))

# 生存分析
# # 安装包
# install.packages("survival", repos="https://cran.cnr.berkeley.edu/")

# # 应用函数
# library("survival")
# # 创建生存对象
# survfit(Surv(pbc$time, pbc$status == 2) ~ 1)
# # 设置文件名
# png(file = "survival.png")
# # 绘制图
# plot(survfit(Surv(pbc$time, pbc$status == 2) ~ 1))
# # 保存
# dev.off()

# # 卡方检验
# library("MASS")
# # 创建数据帧
# car.data <- data.frame(Cars93$AirBags, Cars93$Type)
# # 创建表
# car.data = table(Cars93$AirBags, Cars93$Type)
# print(car.data)
# # 卡方校验
# print(chisq.test(car.data))


















